package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ArticleMapper;
import com.think.teaching.mapper.NewsMapper;
import com.think.teaching.model.Article;
import com.think.teaching.model.ArticleTag;
import com.think.teaching.model.News;
import com.think.teaching.model.NewsTag;
import com.think.teaching.model.Tag;
import com.think.teaching.service.ArticleService;
import com.think.teaching.service.NewsService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class NewsServiceImpl extends BaseServiceImpl<NewsMapper, News> implements NewsService {
	
	private final NewsTagServiceImpl newsTagService;
	
	private final TagServiceImpl tagService;

	public NewsServiceImpl(NewsTagServiceImpl newsTagService, TagServiceImpl tagService) {
		this.newsTagService = newsTagService;
		this.tagService = tagService;
	}

	@Override
	@Transactional
	public News AddNews(News news) throws Throwable {
		
		if (this.save(news)) 
		{
			if (news.getTagIds()!=null && news.getTagIds().size()>0) {
				List<Tag> newTags = tagService.listByIds(news.getTagIds());
				for (Tag newTag : newTags) {
					if (!newTag.getType().equals(Tag.NEWS_TAG)) {
						throw new Exception("传入的标签类型错误，不是文章标签");
					}
				}
				List<NewsTag> tags = new ArrayList<>();
				for (int i = 0; i < news.getTagIds().size(); i++) {
					NewsTag tag = new NewsTag();
					tag.setNewsId(news.getId());
					tag.setTagId(news.getTagIds().get(i));
					
					tags.add(tag);
				}
				newsTagService.saveBatch(tags);
			}
			return news;
		}
		return null;
	}

	public int GetNewsCountByTitle(String title) {
		//判断是否存在同名文章
       	return baseMapper.getSameTitleNewsNums(title);
	}
	

	@Override
	public News UpdateNews(News news) throws Throwable {
		if (this.updateById(news)) {
			if (news.getTagIds()!=null && news.getTagIds().size()>0) {
				List<Tag> newTags = tagService.listByIds(news.getTagIds());
				for (Tag newTag : newTags) {
					if (!newTag.getType().equals(Tag.NEWS_TAG)) {
						throw new Exception("传入的标签类型错误，不是文章标签");
					}
				}
				List<NewsTag> tags = new ArrayList<>();
				for (int i = 0; i < news.getTagIds().size(); i++) {
					NewsTag tag = new NewsTag();
					tag.setNewsId(news.getId());
					tag.setTagId(news.getTagIds().get(i));
					
					tags.add(tag);
				}
				
				//删除原来的
				QueryWrapper<NewsTag> delete = new QueryWrapper<>();
				delete.eq("news_id", news.getId());
				newsTagService.remove(delete);
				newsTagService.saveBatch(tags);
			}
			return news;
		}
		return null;
	}

	@Override
	public News GetNews(Integer id,Boolean withState) {

		return baseMapper.getNewsDetailes(id,withState?1:0);
	}
	
	public List<News> GetRecommendNews(Integer count) {

		QueryWrapper<News> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).eq("state", 1).orderByDesc("created_time");
		queryWrapper.last("limit " + count);
		return list(queryWrapper);
	}

	@Override
	public List<News> ListNewsSearch(String key, Boolean withoutBan) {
		QueryWrapper<News> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("content", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	public IPage<News> ListNewsWithTagAndType(Integer page, Integer count, Integer type, Integer tag,
			Boolean withoutBan,Integer dataType) {
		IPage<News> ipage = new Page<>(page, count);
		return baseMapper.getNewsListWithFilter(ipage,type,tag,withoutBan?1:0,dataType);
	}

	@Override
	public News SetNewsState(Integer id, Integer state) {
		UpdateWrapper<News> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}
}
